from django.db import models
from rbac.models import AbstractUserInfo
from django.utils.safestring import mark_safe


class Department(models.Model):
    """
    部门
    """

    title = models.CharField(verbose_name='部门名称', max_length=32)

    def __str__(self):
        return self.title


class UserInfo(AbstractUserInfo):
    """
    用户
    """
    depart = models.ForeignKey(verbose_name='部门', to='Department')
    email = models.EmailField(verbose_name='邮箱')
    level_choices = (
        (1, 'T1'),
        (2, 'T2'),
        (3, 'T3'),
        (4, 'T4'),
        (5, 'T5'),
        (6, 'T6'),
        (7, 'T7'),
        (8, 'T8'),
        (9, 'T9'),
        (10, 'T10'),
    )
    level = models.IntegerField(verbose_name='级别', choices=level_choices, default=1)

    def __str__(self):
        return self.nickname


class Project(models.Model):
    """
    项目
    """
    title = models.CharField(verbose_name='项目名称', max_length=32)
    color_choices = (
        ('color-blue', mark_safe('<i class="fa fa-circle color-blue" aria-hidden="true"></i>')),
        ('color-orange', mark_safe('<i class="fa fa-circle color-orange" aria-hidden="true"></i>')),
        ('color-yellow', mark_safe('<i class="fa fa-circle color-yellow" aria-hidden="true"></i>')),
        ('color-grassgreen', mark_safe('<i class="fa fa-circle color-grassgreen" aria-hidden="true"></i>')),
        ('color-green', mark_safe('<i class="fa fa-circle color-green" aria-hidden="true"></i>')),
        ('color-lavender', mark_safe('<i class="fa fa-circle color-lavender" aria-hidden="true"></i>')),
        ('color-purple', mark_safe('<i class="fa fa-circle color-purple" aria-hidden="true"></i>')),
    )

    color = models.CharField(verbose_name='背景色', max_length=32)

    content = models.TextField(verbose_name='项目描述', null=True, blank=True)
    members = models.ManyToManyField(verbose_name='成员', to='UserInfo', related_name='projects')

    creator = models.ForeignKey(verbose_name='创建者', to='UserInfo')

    def __str__(self):
        return self.title


class Task(models.Model):
    """
    任务
    """
    project = models.ForeignKey(verbose_name='项目', to='Project')
    title = models.CharField(verbose_name='主题', max_length=32)
    content = models.TextField(verbose_name='描述')

    priority_choice = (
        (1, '低'),
        (2, '中'),
        (3, '高'),
    )
    priority = models.IntegerField(verbose_name='状态', choices=priority_choice)
    status_choices = (
        (1, '已创建'),
        (2, '执行中'),
        (3, '已解决'),
        (4, '已忽略'),
        (11, '已关闭'),
    )
    status = models.IntegerField(verbose_name='状态', choices=status_choices)
    killer = models.ManyToManyField(verbose_name='负责人', to='UserInfo')
    finished = models.DateField(verbose_name='结束时间')
    finish_time = models.DateTimeField(verbose_name='实际结束时间')

    def __str__(self):
        return self.title


class TaskRecord(models.Model):
    """
    任务执行记录
    """
    task = models.ForeignKey(verbose_name='任务', to='Task')
    content = models.TextField(verbose_name='内容')
    create_time = models.DateTimeField(verbose_name='创建时间')

    def __str__(self):
        return self.task.title
